package com.polaris.live.dialog

import android.content.Context
import android.graphics.Color
import android.view.View
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.DateUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.DialogLiveHangUpBinding
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.resp.back_resp.LiveSubsidyIndicatorResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.XPopupUtils

/**
 * LiveHangUpDialog
 *
 * @author Created by 半仙 on 2023/10/11/011 14:30
 */
class LiveHangUpDialog(
    context: Context,
    private val startLiveTime: Long,
    private val liveType: Int,
    private val subsidy: LiveSubsidyIndicatorResp? = null,
    private val onCloseLiveCallBack: () -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_live_hang_up
    }

    private lateinit var mBinding: DialogLiveHangUpBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogLiveHangUpBinding.bind(popupImplView)

        mBinding.apply {
            if (subsidy != null) {
                if (subsidy.endTime >= CacheUtil.getServerTime() && subsidy.show) {
                    setLiveEnd()
                } else {
                    setStartTime(startLiveTime)
                }
            } else {
                setCloseRoom()
            }
            cancel.setOnSingleClickListener {
                dismiss()
            }
            closeLive.setOnSingleClickListener {
                onCloseLiveCallBack.invoke()

                dismiss()
            }
            closeLive2.setOnSingleClickListener {
                onCloseLiveCallBack.invoke()
                dismiss()
            }
        }
    }

    fun setStartTime(startLiveTime: Long) {
        val time = CacheUtil.getServerTime() - startLiveTime

        val (mTitle, mTime) = when (liveType) {
            LiveConst.Type.LIVE_AND_MATCH -> {
                StringUtils.getString(string.live_and_match_end_sure) to StringUtils.getString(
                    string.living_time
                )
            }

            LiveConst.Type.ONLY_LIVE -> {
                StringUtils.getString(string.live_end_sure) to StringUtils.getString(string.living_time)
            }

            LiveConst.Type.PARTY_LIVE, LiveConst.Type.PARTY_GAME_LIVE -> {
                StringUtils.getString(string.sure_close_room) to StringUtils.getString(string.living_time)
            }

            else -> {
                StringUtils.getString(string.match_end_sure) to StringUtils.getString(string.match_time)
            }
        }

        mBinding.tvTitle.text = mTitle

        val span = SpanUtils.with(mBinding.liveTime)
            .append(mTime)
            .setForegroundColor(Color.parseColor("#202028"))
            .append(" ${DateUtils.formatDuration(time)}")
            .setForegroundColor(Color.parseColor("#FE1849"))
            .create()

        mBinding.liveTime.text = span
        mBinding.closeLive.visibility = View.VISIBLE
        mBinding.closeLive2.visibility = View.GONE
    }

    fun setLiveEnd() {
        mBinding.tvTitle.text = StringUtils.getString(string.sure_get_out)
        mBinding.liveTime.text = StringUtils.getString(string.just_a_lettle_get_beans)
        mBinding.cancel.text = StringUtils.getString(string.keep_live)
        mBinding.closeLive.visibility = View.GONE
        mBinding.closeLive2.visibility = View.VISIBLE
    }

    fun setCloseRoom() {
        mBinding.tvTitle.text = StringUtils.getString(string.are_you_sure_leave_room)
        mBinding.closeLive.visibility = View.GONE
        mBinding.liveTime.visibility = View.GONE
        mBinding.closeLive2.visibility = View.VISIBLE
        mBinding.closeLive2.text = StringUtils.getString(string.upgrade_confirm)
    }

    /**
     * 是否已经创建
     */
    fun isCreated(): Boolean {
        return this::mBinding.isInitialized
    }

    companion object {

        /**
         * 显示dialog
         */
        fun showDialog(
            context: Context,
            liveHangUpDialog: LiveHangUpDialog?,
            liveInfo: LiveRoomInfo,
            subsidy: LiveSubsidyIndicatorResp?,
            isDisposable: Boolean = false,
            onStopLive: (Boolean) -> Unit,
        ): LiveHangUpDialog {
            val finalLiveHangUpDialog =
                if (liveHangUpDialog == null || !liveHangUpDialog.isCreated()) {
                    LiveHangUpDialog(context, liveInfo.startTime, liveInfo.type, subsidy) {
                        onStopLive(true)
                    }
                } else {
                    if (subsidy != null) {
                        if (subsidy.endTime != null && subsidy.endTime < CacheUtil.getServerTime() && subsidy.show) {
                            liveHangUpDialog.setLiveEnd()
                        } else {
                            liveHangUpDialog.setStartTime(liveInfo.startTime)
                        }
                    } else {
                        liveHangUpDialog.setCloseRoom()
                    }

                    liveHangUpDialog
                }

            return finalLiveHangUpDialog.also {
                XPopupUtils.showViewPopup(
                    it,
                    isDisposable = isDisposable
                )
            }
        }
    }
}